﻿<?xml version="1.0" encoding="utf-8" ?>
<AdoNetDataAccess connection="$ArticleDBConnection$" formatter="bizobjlist of Article">
	<FormatterInfo>
		<code for="vars" on="Article">
			int firstDepth = -1;
			bool resetDepth = false;
			bool stripLeading = true;
			bool setup = false;
		</code>

		<code for="postEach" on="Article">
			<![CDATA[
		try
		{
			if(!setup)
			{        
			  if(info.get("stripLeading") is bool)
				stripLeading = (bool)info.get("stripLeading");
			  else if(info.get("stripLeading") is string)
				stripLeading = (string.Compare((string)info.get("stripLeading"), "False", true) == 0)?false:true;
			  resetDepth = info.get("resetDepth") is string && string.Compare((string)info.get("resetDepth"), "true", true) == 0;
			  setup = true;
			}
          
			if(CurrentBizObj.get("mediaCacheName").ToString().Length==0)
				CurrentBizObj.put("mediaCacheName",info.get("defaultCacheName"));
				
			if(stripLeading)
			{
			  string title = (string)CurrentBizObj.get("baseTitle");
			  int titIndex = title.IndexOf("~!");
			  if(titIndex != -1)
				CurrentBizObj.put("baseTitle", title.Substring(titIndex+2));
			}
		}
		catch(Exception e)
		{
		}
			 
		try
		{
			//The problem with the following 3 lines is that when we chunk the articles
			//we dont know what the real start dept is.  We need this to be a parameter into
			//the call so we know what the true start depth really is.
			if(resetDepth)
			{
				  if(firstDepth == -1)
					  firstDepth = (int)CurrentBizObj.get("depth");
				  CurrentBizObj.put("depth", (int)CurrentBizObj.get("depth") - firstDepth);						
	        }
		}
		catch(Exception e)
		{
		
		}			
		]]>
		</code>
	</FormatterInfo>

	<Sql>
declare @newSearch nvarchar(50)
set @newSearch = '""' + @search + '*""';

	SELECT
			a.id, t.parentId, baseTitle, translatedTitles, translationDetails, c.keyName
		FROM
			Articles as a
			INNER JOIN FREETEXTTABLE(Articles, baseTitle, @newSearch,300) AS key_tbl on a.id = key_tbl.[KEY]
			INNER JOIN ObjectTreeCounts c on a.id = c.objectId
			INNER JOIN ObjectTreeNodes t on t.objectId = a.id
			
		WHERE
			companyId = @companyId AND treeName = @treeName
		<code><![CDATA[
			if(info.get("parentId") != null)
				sql.Append("AND t.parentHostId = @parentId");	
		]]></code>
		ORDER BY
			LEN(baseTitle)
	</Sql>
	<Param name="companyId" type="UniqueIdentifier" />
	<Param name="treeName" type="String" />
	<Param name="search" type="String" />
	<Param name="parentId" type="UniqueIdentifier" />
</AdoNetDataAccess>
